package lambda.function;
import lambda.entity.User;
import java.util.function.UnaryOperator;

/**
 * @author LIBOFEI
 * @ClassName UnaryOperatorTest
 * @description UnaryOperator<T>
 *     参数为 T，对参数为T的对象进行一元操作，并返回T类型结果
 *      方法为 T apply(T t)
 * @date 2022/4/6 17:09
 * @Version 1.0
 **/
public class UnaryOperatorTest {

    public static void main(String[] args) {
        //未使用lamda表达式之前
        User test = test(new UnaryOperator<User>() {
            @Override
            public User apply(User user) {
                user.setId(user.getId() + 1);
                user.setName(user.getName());
                return user;
            }
        });
        System.out.println(test);

        User test1 = test(user -> {
            user.setId(user.getId() + 3);
            user.setName(user.getName());
            return user;
        });

        System.out.println(test1);
    }


    public static User getUser(int id,String name){
        User user = new User();
        user.setId(id);
        user.setName(name);
        return user;
    }


    public static User test(UnaryOperator<User> userUnaryOperator){
        User u = getUser(1, "不达意");
        User apply = userUnaryOperator.apply(u);
        return apply;
    }
}
